home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DICACHE.C < prev    next >
C/C++ Source or Header  |  1995-09-11  |  4KB  |  113 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    dicache.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains routines to assist in caching of blocked files.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Release cache for file
  49. //    Parameters:    
  50. //       Returns:    TRUE if successful.
  51. //----------------------------------------------------------------------------
  52. BOOL FN_E DioCacheRelease(HLF hlf)
  53. {
  54.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  55.     Assert(di.logical[hlf].fUsed);
  56.  
  57.     if (di.logical[hlf].pcache)                // Free cache if allocated
  58.         MemFree(di.logical[hlf].pcache);
  59.  
  60.     di.logical[hlf].pcache = NULL;            // Reset cache vars
  61.     di.logical[hlf].cCache = 0;
  62.  
  63.     return TRUE;
  64. }
  65.  
  66.  
  67. //----------------------------------------------------------------------------
  68. //   Description:    Set caching buffers for blocked files.
  69. //                          Caching only applies to blocked files.
  70. //                        No error is displayed if allocation of caching buffers
  71. //                        fails, 
  72. //                        Buffers are released if low memory flag is set.
  73. //    Parameters:    hlf        Logical file to cache.
  74. //                        cBuffers    Number of cache buffers to allocate
  75. //       Returns:    TRUE if successful.
  76. //----------------------------------------------------------------------------
  77. BOOL FN_E DioCacheSet(HLF hlf, SIZET cBuffers)
  78. {
  79.     SIZET cb, cbBlock,cbMax;
  80.         
  81.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  82.     Assert(di.logical[hlf].fUsed);
  83.                                       
  84.     if (!DioCacheRelease(hlf))                // Release existing cache
  85.         return FALSE;
  86.     //
  87.     //    File can not be cached if not blocked or if writeable.
  88.     //    Do not cache if low on memory
  89.     //
  90.     if (MemIsLowMemory()
  91.     || !cBuffers
  92.     || !di.logical[hlf].usBlockSize)
  93.         return FALSE;
  94.                                                     // Calculate sizes and limits
  95.     cbBlock = (SIZET)di.logical[hlf].usBlockSize;
  96.     cbBlock += sizeof(CACHE) - sizeof(BYTE);
  97.     cbMax = 32767 / cbBlock; //rather then using 32 _K which will give a
  98.                              // value of 32768 (1 too many), set it to 32767
  99.     if (cBuffers > cbMax)
  100.         cBuffers = cbMax;
  101.     cb = cbBlock * cBuffers;
  102.                                                     // Allocate cache
  103.     di.logical[hlf].pcache = MemAllocZero(cb);
  104.     if (di.logical[hlf].pcache == NULL)
  105.         return FALSE;
  106.     
  107.     di.logical[hlf].cCache = cBuffers;
  108.     return TRUE;
  109. }
  110. //----------------------------------------------------------------------------
  111. //------------------------------- End of File --------------------------------
  112. //----------------------------------------------------------------------------
  113.